//
//  MusicViewController.swift
//  groupTasks
//
//  Created by 向银泉 on 16/11/1.
//  Copyright © 2016年 向银泉. All rights reserved.
//

import UIKit

class MusicViewController: UIViewController,UISearchBarDelegate {
    var scrollView = UIScrollView()
    var lineView = UIView()
    var buttonArray = NSMutableArray()
    override func viewWillAppear(animated: Bool) {
        let firstBtn = self.buttonArray.firstObject as! UIButton
        firstBtn.selected = true
    }
    override func viewDidLoad() {
        super.viewDidLoad()
       self.searchBarSetting()
        self.createUI()
    }
    func searchBarSetting(){
        let searchBar = UISearchBar.init(frame: CGRectMake(100, 0, 150, 40))
        searchBar.showsCancelButton = true
        searchBar.placeholder = "输入艺人、MV或悦单名称"
        searchBar.delegate = self
        self.navigationItem.titleView = searchBar
    }
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
    }
    func createUI() {
        let titleArray = ["大陆","日本","港台","欧美","韩国"]
        for i in 0..<titleArray.count {
            let headerButton = UIButton(type: .Custom)
            headerButton.frame = CGRectMake(CGFloat(i) * Screen_W / CGFloat(titleArray.count), 64, Screen_W / CGFloat(titleArray.count), 60)
            headerButton.setTitle(titleArray[i], forState: .Normal)
            headerButton.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
            headerButton.setTitleColor(UIColor.greenColor(), forState: .Selected)
            headerButton.tag = 10 + i
            headerButton.addTarget(self, action: #selector(headerButtonClick(_:)), forControlEvents: .TouchUpInside)
            self.view.addSubview(headerButton)
            self.buttonArray.addObject(headerButton)
        }
        
        //选中指示条
        lineView = UIView(frame: CGRectMake(0,110,Screen_W / CGFloat(titleArray.count),2))
        lineView.backgroundColor = UIColor.greenColor()
        self.view.addSubview(lineView)
        self.scrollView = UIScrollView(frame: CGRectMake(0,130,Screen_W,Screen_H - 60))
        self.scrollView.pagingEnabled = true
        self.scrollView.delegate = self
        self.view.addSubview(self.scrollView)
        self.scrollView.contentSize = CGSizeMake(Screen_W * CGFloat(titleArray.count), 0)
        self.automaticallyAdjustsScrollViewInsets = false
        //实例化子控制器
        let japaVC = japanViewController()
        let koreVC = koreViewController()
        let eourpVC = eourpViewController()
        let chinaVC = chinaViewController()
        let hongkongVC = hongkongViewController()
        let vcArray = [chinaVC,japaVC,hongkongVC,eourpVC,koreVC]
        for i in 0..<vcArray.count {
            let vc = vcArray[i]
            vc.view.frame = CGRectMake(CGFloat(i) * Screen_W, 0, Screen_W, Screen_H - 60)
            self.addChildViewController(vc)
            self.scrollView.addSubview(vc.view)
        }
        
    }

    
}
extension MusicViewController:UIScrollViewDelegate{
    
    func headerButtonClick(button:UIButton) {
            for btn in self.buttonArray {
                (btn as! UIButton).selected = false
            }
            button.selected = true
            UIView.animateWithDuration(0.3) {
                self.lineView.frame = CGRectMake(CGFloat(button.tag - 10) * Screen_W / 5, 110, Screen_W / 5, 2)
            }
            self.scrollView.setContentOffset(CGPointMake(CGFloat(button.tag - 10) * Screen_W, 0), animated: true)
    }
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = scrollView.contentOffset.x / Screen_W
        let btn = self.buttonArray[Int(index)] as! UIButton
            for btn in self.buttonArray {
                (btn as! UIButton).selected = false
            }
        btn.selected = true
        UIView.animateWithDuration(0.3) {
            self.lineView.frame = CGRectMake(index * Screen_W / 5, 110, Screen_W / 5, 2)
        }
    }
    

}





